package com.controller;

import com.entity.Response;
import com.entity.pojo.LfUser;
import com.service.UserService;
import com.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class LoginController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Response userLogin(@RequestBody LfUser lfUser) {
        if (userService.userLogin(lfUser)) {
            String token = JwtUtil.createToken(lfUser.getUsername());
            String role = userService.getRole(lfUser.getUsername());
            return new Response().setSuccess().appendMsg("token", token).appendMsg("role", role);
        } else {
            return new Response().setFailed().setErrMsg("用户名或密码错误");
        }
    }
}
